package com.example.nonggo.model;


import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.utils.NetUtils;
import com.example.nonggo.api.BaseApi;
import com.example.nonggo.api.MDataObserver;
import com.example.nonggo.bean.LoginNameRes;
import com.example.nonggo.bean.LoginRes;
import com.example.nonggo.contract.LocationContract;
import com.example.nonggo.contract.LoginContract;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;

/**
 * @Author Kotlin MVP Plugin
 * @Date 2025/03/01
 * @Description input description
 **/

public class MainModel extends BaseModel {
    private final String aiURL= "https://zhiwen.mancloud.cn/api/app-api/v1/mojiweather/getLocationName";

    public void getVerCode(String num, LoginContract.Presenter callback) {
        RxHttpUtils.
                createApi(BaseApi.class)
                .getSmsCode(getVerCodeBody(num))
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<>() {

                    @Override
                    protected void onSuccess(Boolean data) {
                            callback.getVerCodeSuccess();
                    }
                    @Override
                    protected void onError(){
                        callback.getVerCodeFail();
                    }
                } );
    }

    public void login(String mobile, String code,LoginContract callback){
        RxHttpUtils.
                createApi(BaseApi.class)
                .login(getLoginBody(mobile,code))
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<LoginRes>(){

                    @Override
                    protected void onSuccess(LoginRes data) {
                        callback.loginSuccess(data);
                    }
                });
    }
    public void getLocationName(String lon, String lat, LocationContract callback){
        RxHttpUtils.
                createApi(BaseApi.class)
                .getLocationName(aiURL,getLocactionBody(lon,lat))
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<LoginNameRes>(){

                    @Override
                    protected void onSuccess(LoginNameRes data) {
                        callback.getLocationSuccess(data);
                    }
                });
    }

    private RequestBody getVerCodeBody(String number) {
        JSONObject jb = new JSONObject();
        try {
            jb.put("scene", 1);
            jb.put("mobile", number);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return getRequestBody(jb);
    }
    private RequestBody getLocactionBody(String lon, String lat) {
        JSONObject jb = new JSONObject();
        try {
            jb.put("lon", lon);
            jb.put("lat", lat);
            jb.put("ip",com.blankj.utilcode.util.NetworkUtils.getIPAddress(true));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return getRequestBody(jb);
    }
    private RequestBody getLoginBody(String mobile, String code) {
        JSONObject jb = new JSONObject();
        try {
            jb.put("mobile", mobile);
            jb.put("code", code);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return getRequestBody(jb);
    }

}